#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include <QLabel>
#include <QKeyEvent>
#include <QTimer>
#include <QPainter>
#include <QPushButton>
#include <QApplication>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    // 游戏状态枚举
    enum GameState {
        START_MENU,  // 起始菜单
        PLAYING,     // 游戏中
        GAME_OVER    // 游戏结束
    };

    // 图片资源
    QPixmap playerIdlePixmap;
    QPixmap playerSwingPixmap;
    QPixmap baseballPixmap;
    QPixmap backgroundPixmap;
    QPixmap startMenuBackground;

    // 棒球主题判定等级枚举
    enum Judgement {
        HOMERUN,    // 全垒打
        GOODBALL,   // 好球
        SINGLEHIT,  // 安打
        FOUL,       // 界外球
        MISS        // 未击中
    };

    // 判定显示结构
    struct JudgementDisplay {
        Judgement type;
        int x, y;
        int timer;
        QString text;
        QColor color;
    };

    QVector<JudgementDisplay> judgementDisplays;

    void drawJudgements(QPainter &painter);

protected:
    void paintEvent(QPaintEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;

private slots:
    void updateGame();
    void startGame();
    void restartGame();
    void exitGame();
    void showLeaderboard();
    void showSettings();

private:
    // 初始化函数
    void initGame();
    void initMenu();
    void initGameOver();

    // 绘制函数
    void drawBackground(QPainter &painter);
    void drawPlayer(QPainter &painter);
    void drawNotes(QPainter &painter);
    void drawUI(QPainter &painter);
    void drawStartMenu(QPainter &painter);
    void drawGameOver(QPainter &painter);

    // 游戏逻辑
    void checkHit(int lane);
    void spawnNote();
    void resetGame();
    void endGame();

    // 游戏状态
    GameState gameState = START_MENU;
    int score = 0;
    int combo = 0;
    int maxCombo = 0;
    int totalNotes = 0;
    int hitNotes = 0;
    int life = 10;
    int maxLife = 10;

    // 游戏计时器
    QTimer *gameTimer = nullptr;

    // 玩家状态
    int playerX = 100;
    int playerY = 450;
    bool isSwinging = false;
    int swingTimer = 0;
    int currentLane = 1;

    // 移动相关变量
    int targetLane = 1;
    int moveSpeed = 8;
    bool isMoving = false;

    // 音符数据结构
    struct Note {
        int lane;    // 轨道编号 (0-3)
        int x;       // x坐标
        bool hit;    // 是否被击中
        bool missed; // 是否错过
    };

    double baseSpeed = 5.0;        // 基础速度
        double speedMultiplier = 1.0;  // 速度倍率
        int minSpawnInterval = 45;     // 最小生成间隔
        int maxSpawnInterval = 75;     // 最大生成间隔
        int nextSpawnThreshold;        // 下次生成阈值

         void updateDifficulty();
    QVector<Note> notes;
    int spawnCounter;

    // 菜单按钮
    QPushButton *startButton = nullptr;
    QPushButton *leaderboardButton = nullptr;
    QPushButton *settingsButton = nullptr;
    QPushButton *exitButton = nullptr;

    // 结算界面按钮
    QPushButton *restartButton = nullptr;
    QPushButton *quitButton = nullptr;
};

#endif // MAINWINDOW_H
